package com.guo.anim.sunset;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;

import androidx.annotation.Nullable;

import com.guo.anim.R;

/**
 * 酷炫的加载中动效
 * @author mailanglideguozhe 20210617
 */
public class SunMoonView extends View {
    /**
     * 太阳，月亮，星星，光线画笔
     */
    private Paint sunPaint , moonPaint , sunshinePaint;

    /**
     * 太阳和月亮相同半径
     */
    private int sunRadius;

    /**
     * 视图的宽度和高度
     */
    private int width,height;
    private final static int MAX_COUNT = 10;
    private int index_draw_sunshine= 0;
    private static final String TAG = "LoadingView";
    private Shape mShape = Shape.SUN;
    public SunMoonView(Context context) {
        super(context);
        initPaint();
    }

    public SunMoonView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public SunMoonView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
    }

    /**
     * 初始化画笔设置
     */
    private void initPaint(){
        if(null == sunPaint){
            sunPaint = new Paint();
            sunPaint.setAntiAlias(true);
            sunPaint.setStyle(Paint.Style.STROKE);
            sunPaint.setStrokeWidth(5);
            sunPaint.setColor(getResources().getColor(R.color.purple_200));
        }
        if(null == moonPaint){
            moonPaint = new Paint();
            moonPaint.setAntiAlias(true);
            moonPaint.setStyle(Paint.Style.STROKE);
            moonPaint.setStrokeWidth(5);
            moonPaint.setColor(getResources().getColor(R.color.purple_200));
        }
        if(null == sunshinePaint){
            sunshinePaint = new Paint();
            sunshinePaint.setAntiAlias(true);
            sunshinePaint.setStyle(Paint.Style.STROKE);
            sunshinePaint.setStrokeWidth(5);
            sunshinePaint.setColor(getResources().getColor(R.color.purple_200));
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth() - getPaddingLeft() - getPaddingRight();
        height = getMeasuredHeight() - getPaddingTop() - getPaddingBottom();
        sunRadius = Math.min(width,height)/5;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mShape){
            case SUN:
            default:
                drawSun(canvas);
                break;
            case MOON:
                drawMoon(canvas);
                break;
        }
    }

    /**
     * 绘制太阳
     * @param canvas
     */
    private void drawSun(Canvas canvas){
        //将画布平移至中心点
        canvas.translate(width/2+getPaddingLeft(),height/2+getPaddingTop());
        //画圆
        canvas.drawCircle(0,0,sunRadius,sunPaint);
        //画光线
        drawSunshine(canvas);
    }

    private void drawSunshine(Canvas canvas){
        /**
         * 逐个绘制光线，绘制10条线
         */
        if(index_draw_sunshine < 10 && index_draw_sunshine >= 0){
            for(int i=0 ;i<=index_draw_sunshine;i++){
                canvas.drawLine(sunRadius+10, 0, sunRadius+30,0,sunshinePaint);
                canvas.rotate(-360/MAX_COUNT);
            }
            index_draw_sunshine++;
            postInvalidateDelayed(20);
        }else{
            for(int i=0 ;i<index_draw_sunshine;i++){
                canvas.drawLine(sunRadius+10, 0, sunRadius+30,0,sunshinePaint);
                canvas.rotate(-360/MAX_COUNT);
            }
        }
    }

    //绘制月亮
    private void drawMoon(Canvas canvas){
        canvas.translate(width/2+getPaddingLeft(),height/2+getPaddingTop());
        //绘制大圆弧
        RectF oval = new RectF(-sunRadius , -sunRadius , sunRadius ,sunRadius);
        canvas.drawArc(oval , -120 , 240 , false , sunPaint);
        //绘制小圆弧
        RectF oval1 = new RectF(-2*sunRadius , -sunRadius , 0 ,sunRadius);
        canvas.drawArc(oval1 , -60 , 120 ,false ,sunPaint);
    }

    public void changeShape(){
        mShape = (mShape == Shape.SUN ? Shape.MOON : Shape.SUN);
        invalidate();
    }

    public Shape getShape() {
        return mShape;
    }

    public enum Shape{
        SUN,
        MOON
    }
}
